package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import modelos.ConexaoDB;
import modelos.Empresa;

public class EmpresaDAO {
	public void CadastrarEmpresa(Empresa emp) throws SQLException{
		Connection conn = new ConexaoDB().pegarconexao();
		PreparedStatement pstm = conn.prepareStatement("insert into empresa (razaosocial, cnpj, telcom, contato, celular) values (?, ?, ?, ?, ?)");
		try{
			pstm.setString(1, emp.getRazaosocial());
			pstm.setString(2, emp.getCnpj());
			pstm.setDouble(3, emp.getTelcom());
			pstm.setString(4, emp.getContato());
			pstm.setDouble(5, emp.getCelular());
			
			pstm.execute();
		}
		catch(SQLException ex){
			throw new RuntimeException(ex);
		}
		finally{
			pstm.close();
		}	
	}
	
	public static List<Empresa> getEmpresa() throws SQLException{
		Connection conn = new ConexaoDB().pegarconexao();
		PreparedStatement pstm = conn.prepareStatement("Select * from empresa");
		List<Empresa> empresas = new ArrayList<Empresa>();
		try{
		ResultSet rs = pstm.executeQuery();
		while(rs.next()){
			Empresa emp = new Empresa();
			emp.setRazaosocial(rs.getString("razaosocial"));
			emp.setCnpj(rs.getString("cnpj"));
			emp.setTelcom(rs.getDouble("telcom"));
			emp.setContato(rs.getString("contato"));
			emp.setCelular(rs.getDouble("celular"));
			
			empresas.add(emp);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}
		return empresas;
	}
}
